//
//  AppDelegate.swift
//  Pz
//
//  Created by 蓝鳍互娱 on 2024/6/26.
//

import UIKit
import CoreData

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow.init(frame: UIScreen.main.bounds)
        window?.backgroundColor = .clear
        
        let homeCtrl = PzHomeCtrl()
        let homeNavCtrl = UINavigationController.init(rootViewController: homeCtrl)
        window?.rootViewController = homeNavCtrl
        window?.makeKeyAndVisible()
        
        let defNum:String? = UserDefaults.standard.value(forKey: "pzDefNum") as? String
        let defAutoNum:String? = UserDefaults.standard.value(forKey: "pzDefAutoNum") as? String
        let defVoiceOpen:String? = UserDefaults.standard.value(forKey: "pzDefVoiceOpen") as? String
        let defFirst:String? = UserDefaults.standard.value(forKey: "pzDefFirst") as? String
        let defTime:Double? = UserDefaults.standard.value(forKey: "pzDefTime") as? Double
        if (defNum == nil){
            UserDefaults.standard.setValue("3", forKey: "pzDefNum")
            UserDefaults.standard.synchronize()
        }
        if (defAutoNum == nil){
            UserDefaults.standard.setValue("1", forKey: "pzDefAutoNum")
            UserDefaults.standard.synchronize()
        }
        if (defVoiceOpen == nil){
            UserDefaults.standard.setValue("1", forKey: "pzDefVoiceOpen")
            UserDefaults.standard.synchronize()
        }
        if (defFirst == nil){
            UserDefaults.standard.setValue("1", forKey: "pzDefFirst")
            UserDefaults.standard.synchronize()
        }
        if (defTime ?? 0 <= 0){
            UserDefaults.standard.setValue(0, forKey: "pzDefTime")
            UserDefaults.standard.synchronize()
        }
        saveBuyVipTime()//免费版
        PzPublicData.shareManage.requestVipPrice()
        return true
    }
    func saveBuyVipTime(){
        let currentDate = Date()
        let calendar = Calendar.current
        var dateComponents = DateComponents()
        dateComponents.year = 1
        if let newDate = calendar.date(byAdding: dateComponents, to: currentDate) {
            let currentTimeStamp = newDate.timeIntervalSince1970
            let timestamp = Double(currentTimeStamp) // 如果需要毫秒精度
            UserDefaults.standard.setValue(timestamp, forKey: "pzDefTime")
            UserDefaults.standard.synchronize()
        }
    }
}

